package com.jiepos.mpos.biz.operationlog.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.dao.AppLogInterDataDao;
import com.jiepos.mpos.common.entity.App_Log_Inter_DataEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.StringUtils;

@Service
public class LogInterDataService extends BaseService {

	@Autowired
	private AppLogInterDataDao logInterDataDao;
	
	/**
	 * 获取接口数据报送记录Json
	 * @param pagination
	 * @param request
	 * @param app_Log_Inter_DataEntity
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Map getloginterdataJson(Pagination pagination,HttpServletRequest request,App_Log_Inter_DataEntity app_Log_Inter_DataEntity,String startDate,String endDate){
		StringBuffer sb = new StringBuffer();
		List list = new ArrayList<>();
		
		sb.append(" SELECT t1.inter_data_uuid,(select code_val||'-'||code_desc from T_APP_CODE_LIST where code_val = t1.inter_type and code_tp = 'INTER_TYPE') inter_type,")
			.append(" (select code_val||'-'||code_desc from T_APP_CODE_LIST where code_val = t1.workflow_type and code_tp = 'WORKFLOW_TYPE') workflow_type,")
			.append("CONCAT(RPAD(SUBSTR(t2.login_id, 1, 3),LENGTH (t2.login_id) - 4,'*'),SUBSTR (t2.login_id,LENGTH (t2.login_id) - 3,4)) ||'-'|| t2.name as mchnt_nm, ")
			.append(" T1.mchnt_cd,T1.trf_mchnt_cd,T1.trf_term_id,T1.req_content,T1.res_content,T1.RSP_CODE ||'-'||T1.RSP_MSG as RSP_CODE,")
			.append(" T1.query_no,T1.audit_msg,to_char(T1.audit_tm,'yyyy-mm-dd hh24:mi:ss') audit_tm,T1.channel_No||'-'||t3.chn_nm as channel_No,")
			.append(" to_char(T1.com_tm,'yyyy-mm-dd hh24:mi:ss') com_tm,T1.rec_upd_usr_id,")
			.append(" to_char(T1.rec_crt_ts,'yyyy-mm-dd hh24:mi:ss') rec_crt_ts from ")
			.append(Table.T_APP_LOG_INTER_DATA).append(" t1 ")
			.append(" left join ")
			.append(Table.T_APP_CUSTOM_INF).append(" t2 ")
			.append(" on t1.mchnt_cd = t2.custom_no ")
			.append(" left join ")
			.append(Table.T_APP_INF_CHANNEL).append(" t3 ")
			.append(" on t1.channel_No = t3.channel_No ")
			.append("  where 1=1 ");
		if(!StringUtils.isEmpty(app_Log_Inter_DataEntity.getInter_type())){
			sb.append(" and t1.inter_type = ? ");
			list.add(app_Log_Inter_DataEntity.getInter_type());
		}
		if(!StringUtils.isEmpty(app_Log_Inter_DataEntity.getWorkflow_type())){
			sb.append(" and t1.workflow_type = ? ");
			list.add(app_Log_Inter_DataEntity.getWorkflow_type());
		}
		if(!StringUtils.isEmpty(app_Log_Inter_DataEntity.getMchnt_cd())){
			sb.append(" and t1.mchnt_cd like ? ");
			list.add("%"+app_Log_Inter_DataEntity.getMchnt_cd()+"%");
		}
		if(!StringUtils.isEmpty(app_Log_Inter_DataEntity.getTrf_mchnt_cd())){
			sb.append(" and t1.trf_mchnt_cd like ? ");
			list.add("%"+app_Log_Inter_DataEntity.getTrf_mchnt_cd()+"%");
		}
		if(!StringUtils.isEmpty(app_Log_Inter_DataEntity.getChannel_No())){
			sb.append(" and t1.channel_No like ? ");
			list.add("%"+app_Log_Inter_DataEntity.getChannel_No()+"%");
		}
		if(!StringUtils.isEmpty(startDate)){
			sb.append(" and to_char(t1.rec_crt_ts,'yyyy-MM-dd') >= ? ");
			list.add(startDate);
		}else{
			sb.append(" and to_char(t1.rec_crt_ts,'yyyy-MM-dd') >= ? ");
			list.add(DateUtil.getCurrDate());
		}
		if(!StringUtils.isEmpty(endDate)){
			sb.append(" and to_char(t1.rec_crt_ts,'yyyy-MM-dd') <= ? ");
			list.add(endDate);
		}else{
			sb.append(" and to_char(t1.rec_crt_ts,'yyyy-MM-dd') <= ? ");
			list.add(DateUtil.getCurrDate());
		}
		
		sb.append(" order by t1.rec_crt_ts desc ");
		pagination.queryString = sb.toString();
		return logInterDataDao.getPaginationMap(pagination, "t1", list);
	}
}
